/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.BookInfo;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.OracleTypes;

/**
 *
 * @author Alena
 */
public class CurrentUserScore {

   public int getCurrentUserScore (String isbn, Connection conn, int userId) throws SQLException {
       
       int rating = 0;
       ResultSet rs = null;
       CallableStatement cStmt = null;
       
       try {
           
            cStmt = conn.prepareCall("{call GET_BOOK_USER_RATING(?,?,?)}");
            cStmt.setString(1, isbn);
            cStmt.setInt(2, userId);
            cStmt.registerOutParameter(3, OracleTypes.INTEGER);          
            cStmt.execute();
            rating = cStmt.getInt(3);
            
       } catch (SQLException ex) {
          if (ex.getMessage().equals("That user does not vote for this book")) {
              rating = 0;
              return rating;
          }         
        } finally {
             try {
                if (rs != null) {
                    rs.close();
                }
            }
            finally {
                    if (cStmt != null) {
                        cStmt.close();                    
                    }
            }
        }
       return rating;
   }    
}
